package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Order;
import com.itheima.domain.Traveller;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.itheima.domain.Member;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    //查询所有信息
    public void list(HttpServletRequest request, HttpServletResponse response) {
        try {
            int page = 1;
            int size = 3;

            if (StringUtils.isNotBlank(request.getParameter("pageNum"))) {
                page = Integer.parseInt(request.getParameter("pageNum"));
            }
            if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
                size = Integer.parseInt(request.getParameter("pageSize"));
            }
            PageInfo all = orderService.findAll(page, size);
            request.setAttribute("page", all);
            request.getRequestDispatcher("/WEB-INF/pages/order/orders-list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除所有选中的信息
    public void deleteByCheck(HttpServletRequest request, HttpServletResponse response){
        try {
            String[] ids = request.getParameterValues("ids");
            orderService.deleteByCheckInOP(ids);
            orderService.deleteByCheck(ids);
            response.sendRedirect(request.getContextPath() + "/order?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询对应id的详情
    public void view(HttpServletRequest request, HttpServletResponse response){
        try {
            String pId = request.getParameter("pId");
            String id = request.getParameter("id");
            /*获取订单信息*/
            Map orderMap = orderService.findById(id, pId);
            request.setAttribute("orderMap", orderMap);

            //获取游客信息
            List<Traveller> travellerList = orderService.findTraveller(id);
            request.setAttribute("travellerList", travellerList);

            //获取会员信息
            String memberId = request.getParameter("memberId");
            Member member = orderService.findMember(memberId);
            request.setAttribute("member", member);

            request.getRequestDispatcher("/WEB-INF/pages/order/orders-show.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改状态
    public void updateStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            String orderStatus = request.getParameter("orderStatus");
            String id = request.getParameter("id");
            orderService.updateStatus(id, orderStatus);
            response.sendRedirect(request.getContextPath() + "/order?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
